<template>
  <div class="edit-container">
    <!-- 导航条 -->
    <van-nav-bar
      left-arrow
      title="编辑资料"
      @click-left="$router.go(-1)"
    ></van-nav-bar>
    <!-- 头像部分 -->
    <div class="avatar">
      <van-image fit="cover" round :src="userInfo.photo" />
      <!-- 头像插件 -->
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 信息展示 -->
    <van-cell-group>
      <van-cell
        is-link
        title="名称"
        :value="userInfo.name"
        @click="editNameClick"
      />
      <van-cell
        is-link
        title="性别"
        :value="userInfo.gender ? '女' : '男'"
        @click="showPopup = true"
      />
      <van-cell
        is-link
        title="生日"
        :value="userInfo.birthday"
        @click="
          showDatePicker = true;
          currentDate = new Date(userInfo.birthday);
        "
      />
    </van-cell-group>
    <!-- 编辑姓名弹窗 -->
    <van-dialog
      v-model="showDialog"
      title=" 编辑姓名"
      show-cancel-button
      @confirm="updateName"
    >
      <van-field ref="nameFiled" placeholder="请输入昵称" v-model="newName" />
    </van-dialog>

    <!-- 编辑性别弹窗 -->
    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar
        title="修改性别"
        left-text="取消"
        left-arrow
        @click-left="showPopup = false"
      />
      <van-cell is-link title="男" @click="updateGender(0)" />
      <van-cell is-link title="女" @click="updateGender(1)" />
    </van-popup>
    <!-- 修改生日 -->
    <van-popup v-model="showDatePicker" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showDatePicker = false"
        @confirm="upDateBirthday"
      />
    </van-popup>
    <!-- 图片裁切功能 -->
    <div class="cropper-box" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropWidth="120"
        autoCropHeight="120"
      ></vueCropper>
      <van-button
        class="confirm-btn"
        type="primary"
        @click="showCropper = false"
        >取消</van-button
      >
      <van-button class="cancel-btn" type="info" @click="upDataPopup"
        >确定</van-button
      >
    </div>
  </div>
</template>

<script>
// 导入： mapState  辅助函数
import { mapState } from "vuex";
// 导入 editUserInfo
import { editUserInfo, editUserPhoto } from "./user.js";
// 时间插件
import * as moment from "moment";
// 图片预览导包
import { VueCropper } from "vue-cropper";
// 按钮导包
// import { Button } from "vant";
export default {
  name: "edit",
  components: {
    VueCropper,
  },
  data() {
    return {
      // 编辑姓名弹窗
      showDialog: false,
      // 姓名输入框
      newName: "",
      // 编辑性别弹窗
      showPopup: false,
      // 选择日期
      minDate: new Date(1960, 0, 1),
      maxDate: new Date(2035, 10, 1),
      currentDate: new Date(),
      // 编辑生日弹窗
      showDatePicker: false,
      // 生日
      img: "",
      // 裁切框弹窗
      showCropper: false,
    };
  },

  methods: {
    // 点击修改名字按钮
    editNameClick() {
      // 弹出 dialog  弹窗
      this.showDialog = true;
      // 设置输入框本文
      this.newName = this.userInfo.name;
      this.$nextTick(() => {
        this.$refs.nameFiled.focus();
      });
    },

    // dislog 弹窗 comfirm 事件
    //2.修改名字
    updateName() {
      editUserInfo({ name: this.newName }).then((res) => {
        console.log(res);
        //(1)弹窗提示
        this.$toast.success("更新成功!");
        //(2)更新vuex数据
        this.$store.commit("updataUserInfo", {
          ...this.userInfo, //先传入原始数据
          name: this.newName, //然后使用新的name覆盖原始的name
        });
      });
    },
    // 3. 修改性别
    updateGender(gender) {
      // 调用接口
      editUserInfo({ gender }).then((res) => {
        // 弹出提示
        this.$toast.success("更新成功");
        // 隐藏 popup 弹窗
        this.showPopup = false;
        // 更新 vuex
        this.$store.commit("updataUserInfo", {
          ...this.userInfo,
          gender,
        });
      });
    },
    // 修改生日
    upDateBirthday() {
      let birthday = moment(this.currentDate).format("YYYY-MM-DD");
      // console.log(birthday);
      editUserInfo({ birthday }).then((res) => {
        // console.log(res);
        this.$toast.success("更新成功");
        // 隐藏 popup 弹窗
        this.showDatePicker = false;
        // 更新 vuex
        this.$store.commit("updataUserInfo", {
          ...this.userInfo,
          birthday,
        });
      });
    },
    // 上传文件
    afterRead(file) {
      // console.log(file);
      this.img = file.content;
      // 弹出裁切框
      this.showCropper = true;
    },
    // 上传裁切后的图片
    upDataPopup() {
      this.$refs.cropper.getCropBlob((data) => {
        // do something
        let fd = new FormData();
        fd.append("photo", data);
        editUserPhoto(fd).then((res) => {
          // console.log(res);
          this.$toast.success("更新成功");
          // 关闭裁切框
          this.showCropper = false;
          // 更新 vuex
          this.$store.commit("updataUserInfo", {
            ...this.userInfo,
            photo: res.data.data.photo,
          });
        });
      });
    },
  },
  // 计算属性
  computed: {
    ...mapState(["userInfo"]),
  },
};
</script>


<style lang="less">
.edit-container {
  // 导航条部分
  .van-nav-bar {
    background-color: #3196fa;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  }
  // 头像部分
  .avatar {
    padding: 20px 0px;
    text-align: center;
    .van-image {
      width: 120px;
      height: 120px;
    }
  }
  //文件上传
  .van-uploader {
    opacity: 0;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    width: 120px;
    height: 120px;
    .van-uploader__upload {
      width: 120px;
      height: 120px;
    }
  }
  // 裁切弹框样式
  .cropper-box {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    .confirm-btn {
      position: absolute;
      left: 0;
      bottom: 0;
    }
    .cancel-btn {
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
}
</style>